//
// Created by 张兮兮 on 2025/3/13.
//

#include "WorkerWidget.h"
#include <QDebug>
#include <QPushButton>
#include <QRandomGenerator>
#include <QPainter>
WorkerWidget::WorkerWidget(QWidget *parent) {
    qDebug() << "WorkerWidget 线程：" << QThread::currentThread();
    QPushButton *button = new QPushButton("释放", this);
    button->setGeometry(10, 10, 100, 40); // 设置按钮位置和大小
    connect(button, &QPushButton::clicked, this, &WorkerWidget::onButtonClicked);




    QPushButton *button_add = new QPushButton("创建", this);
    button_add->setGeometry(130, 10, 100, 40); // 设置按钮位置和大小
    connect(button_add, &QPushButton::clicked, this, [this](){
        if (worker != nullptr){
            qDebug()<<"worker还在";
        }
        if (workerThread.isRunning()){
            qDebug()<<"workerThread还在走";
        }
        if (worker == nullptr && !workerThread.isRunning()){
            worker = new  Worker();
            worker->moveToThread(&workerThread);
            connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
            connect(this, &WorkerWidget::sendImageToWorker, worker, &Worker::processImage);
            connect(this, &WorkerWidget::initWorker, worker, &Worker::init);
            connect(this, &WorkerWidget::stopWorker, worker, &Worker::stop);
            connect(worker, &Worker::resultReady, this, &WorkerWidget::handleResult);
            connect(worker, &Worker::workerStopped, this, &WorkerWidget::onWorkerStopped);  // 监听 workerStopped 信号
            workerThread.start();
            emit initWorker();
        }
    });


//    QPushButton *button = new QPushButton("重新", this);
//    button->setGeometry(10, 10, 100, 40); // 设置按钮位置和大小
//    connect(button, &QPushButton::clicked, this, &WorkerWidget::onButtonClicked);

    image.load("/Users/zhanghongwei/Desktop/temp/hand02.jpg");



    connect(&timer, &QTimer::timeout, this, &WorkerWidget::sendImage);
    timer.start(33);  // 每 33ms 发送一张图片

}



void WorkerWidget::onButtonClicked() {
    qDebug() << "释放线程";
//    timer.stop();
    emit stopWorker();
}


void WorkerWidget::onWorkerStopped() {
    qDebug() << "Worker 停止，准备释放线程和 Worker";
    disconnect();
    workerThread.quit();  // 退出线程
    workerThread.wait();  // 等待线程退出
    worker = nullptr;
    qDebug() << "Worker 和 WorkerThread 已删除";

}



void WorkerWidget::sendImage() {
    qDebug()<<"WorkerWidget::sendImage";
    emit sendImageToWorker(image);
    int r = QRandomGenerator::global()->bounded(256);  // 红色
    int g = QRandomGenerator::global()->bounded(256);  // 绿色
    int b = QRandomGenerator::global()->bounded(256);  // 蓝色
    currentFrame = QImage(800, 600, QImage::Format_RGB32);
    currentFrame.fill(QColor(r, g, b)); // 用随机颜色填充帧
    update();
}

void WorkerWidget::handleResult(const QString &result) {
    qDebug() << "处理结果：" << result;
}
WorkerWidget::~WorkerWidget() {
//    onButtonClicked();
}

void WorkerWidget::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    if (!currentFrame.isNull()) {
        painter.drawImage(0, 0, currentFrame);
    }
}